<?php

// refactor: no component! doesnt have a view for ajax
// refactor: start month with 1 not 0
class CalendarComponent extends Object{ 

//	var $components = array('Auth');

	function beforeFilter() {

		$this->Auth->allow('*');
		parent::beforeFilter();
		global $sLang;
		$this->set('sLang', $sLang);
	}


	var $lsDays = array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
	var $lnMonthDays = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var $lsMonths = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
	var $nYear = null;
	var $nMonth = null;
	var $nDay = null;
	var $oEventDate = null;
	var $aDates = null; 
	var $Text = null;

	function startup(&$Controller){

		$this->oEventDate = $Controller->EventDate;
	}

	function getCalendar($nYear = null, $nMonth = null){

		$this->nYear = ($nYear === null)
			? date('Y')
			: $nYear;
		$this->nMonth = ($nMonth === null) // refactor: do it the other way around.. and add 1
			? date('n')
			: $nMonth;
		$this->nMonth--;
		$this->setFebruary();
		$this->setFirstDay();
		$this->getEvents();
		return $this->createCalendar();
	}

	function getEvents(){

		$dateFrom = ($this->nYear.'-'.($this->nMonth + 1).'-01'); // refactor: convert to a date object ?
		$dateTo = ($this->nYear.'-'.($this->nMonth + 1).'-'.$this->lnMonthDays[$this->nMonth]);
		$sFilter = " date >= ('$dateFrom') AND date <= ('$dateTo') ";
		$lFields = array('date', 'EventDate.id', 'Event.category_id'); //, 'Event.textTitle');
		$aDates = $this->oEventDate->findAll($sFilter, $lFields, null);
		$num = count($aDates);
		for($x=0; $x<$num; $x++){
			$date = strtotime($aDates[$x]['EventDate']['date']);
			$nDay = date('j', $date);
			$nId = $aDates[$x]['EventDate']['id'];
			$categoryId = $aDates[$x]['Event']['category_id'];
			$sText = 'asdf'; //$aDates[$x]['Event']['textTitle'];
			$this->aDates[$nDay] = array($nId, $categoryId, $sText);
		}
	}

	function getEventPath($nId, $categoryId){

		$path  = '/cake/controller/index/'; // refactor: use global vars for path
		$path .= "events/$categoryId/$nId/";
		return $path;
	}

	function createCalendar(){

		$html  = '<table id="calendar">';
		$html .= '<col class="weekend"/><col/><col/><col/><col/><col/><col class="weekend"/>';
		$html .= $this->htmlGetTableCaption($this->nMonth, $this->nYear);
		$html .= $this->htmlGetTableHeader();
		$html .= '<tbody><tr>';
		$html .= $this->htmlGetEmtyCells($this->nDay);
		for ($xDay = 1; $xDay<$this->lnMonthDays[$this->nMonth] + 1; $xDay++){
			$html .= $this->htmlGetTableCell($xDay);
			$this->nDay++;
			if ($this->nDay == 7){
				$html .= "</tr><tr>";
				$this->nDay = 0;
			}
		}
		if ($this->nDay > 0){
			$html .= $this->htmlGetEmtyCells(7-$this->nDay);
		}
		$html .= "</tr></tbody>";
		$html .= "</table>";
		return $html;
	}

	function setFirstDay(){

		$nDay = 6;
		for ($j = 1600; $j < ($this->nYear - 1); $j++){
			$nDay++;
			if(($j % 400 == 0) || ($j % 4 == 0 && $j % 100 <> 0)){
				$nDay++;
			}
		}
		for($x = 0; $x<$this->nMonth; $x++){
			$nDay += $this->lnMonthDays[$x];
		}
		$nDay = ($nDay % 7) + 1;
		$this->nDay = $nDay;
	}

	function setFebruary(){

		if(($this->nYear % 400 == 0) || ($this->nYear % 4 == 0 && $this->nYear % 100 <> 0)){
			$this->lnMonthDays[1] = 29;
		}
	}

	function htmlGetEmtyCells($n){

		$html = '';
		for($x=0; $x<$n; $x++){
			$html .= '<td class="empty">&nbsp;</td>';
		}
		return $html;
	}

	function htmlGetTableCell($nDay){

		$html = '';
		if(empty($this->aDates[$nDay])){
			$html .= "<td>$nDay</td>";
		}
		else{
			$html .= "<td>";
			$html .= "<a href='".$this->getEventPath($this->aDates[$nDay][0], $this->aDates[$nDay][1])."'";
			$html .= " title='".$this->aDates[$nDay][2]."'>";
			$html .= "$nDay</a>";
			$html .= "</td>";
		}
		return $html;
	}

	function htmlGetTableHeader(){

		$html = '<thead><tr>';
		$num = count($this->lsDays);
		for($x = 0; $x < $num; $x++){
			$html .= '<th>'.$this->lsDays[$x].'</td>';
		}
		$html .= '</tr></thead>';
		return $html;
	}

	function htmlGetTableCaption(){

		$html = '<caption>';
		$html .= $this->lsMonths[$this->nMonth].' '.$this->nYear;
		$html .= '</caption>';
		return $html;
	}

}

?>
